//
//  SEGCommunitySearchView.m
//  SEGUhomeStandard
//
//  Created by 揭婧雯 on 2018/8/14.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGCommunitySearchView.h"

static const CGFloat textFieldHeight = 35.0;
static const CGFloat imageTitleSpace = 5.0;
@interface SEGCommunitySearchView ()


@end

@implementation SEGCommunitySearchView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    self.backgroundColor = kColor_W;
    [self addSubview:self.cityButton];
    CGFloat width = [self.cityButton sizeThatFits:CGSizeMake(MAXFLOAT, self.cityButton.titleLabel.font.lineHeight)].width + imageTitleSpace;
    [self.cityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.bottom.equalTo(self);
        make.height.equalTo(self);
        make.width.mas_equalTo(width);
    }];
    
    [self addSubview:self.communityNameText];
    [self.communityNameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cityButton.mas_right).offset(15).priorityHigh();
        make.centerY.equalTo(self);
        make.width.mas_equalTo(kScreenWidth-45-width);
        make.height.mas_equalTo(textFieldHeight);
    }];
    
//    self.cityButton.backgroundColor = kColor_T;
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
}

- (void)setCityName:(NSString *)cityName
{
    [self.cityButton setTitle:cityName forState:UIControlStateNormal];
    [self.cityButton layoutButtonWithEdgeInsetsStyle:SEGButtonEdgeInsetsStyleRight imageTitleSpace:imageTitleSpace];
    
    
    CGFloat width = [self.cityButton sizeThatFits:CGSizeMake(MAXFLOAT, self.cityButton.titleLabel.font.lineHeight)].width + imageTitleSpace;
    [self.cityButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
    }];
    [self.communityNameText mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth-45-width);
    }];
}

- (SEGAdjustableButton *)cityButton
{
    if (!_cityButton) {
        _cityButton = [[SEGAdjustableButton alloc] init];
//        [_cityButton setTitle:@"深圳市" forState:UIControlStateNormal];//默认
        [_cityButton setTitleColor:kColor_1 forState:UIControlStateNormal];
        _cityButton.titleLabel.font = kFont_16;
        [_cityButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_search_default"]) forState:UIControlStateNormal];
        [_cityButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_search_packup"]) forState:UIControlStateSelected];
        [_cityButton layoutButtonWithEdgeInsetsStyle:SEGButtonEdgeInsetsStyleRight imageTitleSpace:imageTitleSpace];
    }
    return _cityButton;
}

- (UITextField*)communityNameText
{
    if (!_communityNameText) {
        _communityNameText = [[UITextField alloc] init];
        

        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_search")];
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width + 15, image.size.height)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, image.size.width, image.size.height)];
        imageView.contentMode = UIViewContentModeCenter;
        [imageView setImage:image];
        [leftView addSubview:imageView];
        _communityNameText.leftView = leftView;
        _communityNameText.leftViewMode = UITextFieldViewModeAlways;
        [_communityNameText setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"请输入社区名称" attributes:@{NSForegroundColorAttributeName:kColor_C}]];
        
        _communityNameText.backgroundColor = kColor_L3;
        [_communityNameText setTextColor:kColor_1];
//        _communityNameText.delegate = self;
        [_communityNameText setFont:kFont_16];
        _communityNameText.layer.cornerRadius = textFieldHeight / 2.0;
        _communityNameText.layer.masksToBounds = YES;
        
        _communityNameText.returnKeyType = UIReturnKeySearch;
        _communityNameText.keyboardType = UIKeyboardTypeDefault;
        [_communityNameText resignFirstResponder];
        [_communityNameText setTintColor:kColor_T];
    }
    return _communityNameText;
}

@end
